<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字</title>
</head>
<body>
    <h3>请输入要猜的数字</h3>
    <input type="text">
    <div>
        已经猜的次数: <span id="guessCount">0</span>
    </div>
    <div>
        结果是: <span id="result"></span>
    </div>
    <button>猜</button>

    <script>
        //0. 获取元素
        let input = document.querySelector('input');
        let button = document.querySelector('button');
        let guessCount = document.querySelector('#guessCount');
        let result = document.querySelector('#result');

        // 1.生成随机数
        let toGuess = parseInt(Math.random() * 100) + 1;
        console.log(toGuess);

        // 2.给 猜  这个按钮添加点击事件
        button.onclick = function() {
            let value = parseInt(input.value);
            if(value < toGuess) {
                result.innerHTML = '低了';
            } else if (value > toGuess) {
                result.innerHTML = '高了';
            }else {
                result.innerHTML = '猜对了!'
            }
            let guessCountValue = parseInt(guessCount.innerHTML);
            guessCount.innerHTML = guessCountValue + 1;
        }

    </script>
</body>
</html>